<?php
switch ($_REQUEST['sAction'])
{
	case 'time-delete':
		$iResult = TimeHandler::delTime($_REQUEST['iTime'], $_REQUEST['iMission']);
		if ($iResult == 1) {
			header('Location: index.php?sView=time-list&iYear='.$_REQUEST['iYear'].'&iMonth='.$_REQUEST['iMonth']);
			exit();
		}
		break;
	case 'time-change':
		$iStartTime = mktime(0, 0, 0, (int)$_REQUEST['iMonth'], (int)$_REQUEST['iDay'], $_REQUEST['iYear']);
		$iResult = TimeHandler::changeTime($_REQUEST['iTime'], $iStartTime, $_REQUEST['iHours'], $_REQUEST['sSerial'], $_REQUEST['sDescription'], $_REQUEST['iMission'], $_REQUEST['bOvertime'], $_REQUEST['bDriving']);
		if ($iResult == 1) {
			header('Location: index.php?sView=time-list&iYear='.$_REQUEST['iYear'].'&iMonth='.$_REQUEST['iMonth']);
			exit();
		}
		break;
	case 'time-add':
		//$iDuration = mktime($_REQUEST['iHours'], $_REQUEST['iMinutes'], 0, 1, 1, 1970);
		$bError = false;
		foreach ($_REQUEST['iHours'] as $sThisDate => $iHours)
		{
			if (!$iHours) {
				continue;
			}
			
			$iDuration = (str_replace(',', '.', $iHours) * 60 * 60);
			list($iYear, $iMonth, $iDay) = split('-', $sThisDate);
			$iYear = (int)$iYear;
			$iMonth = (int)$iMonth;
			$iDay = (int)$iDay;
			$iStartTime = mktime(0, 0, 0, $iMonth, $iDay, $iYear);
			if ($_REQUEST['iDriving'] > 0) {
				$iDriveDuration = $_REQUEST['iDriving'] * 3600;
				$iResult = TimeHandler::addTime($_REQUEST['iMissionID'], $_REQUEST['iJob'], $iStartTime, $iDriveDuration, $_REQUEST['sSerial'], $_REQUEST['sDescription'], $_REQUEST['bOvertime'], true, $_REQUEST['iClientgroup']);
				if ($iResult <= 0) {
					$bError = true;
				}
			}
			$iResult = TimeHandler::addTime($_REQUEST['iMissionID'], $_REQUEST['iJob'], $iStartTime, $iDuration, $_REQUEST['sSerial'], $_REQUEST['sDescription'], $_REQUEST['bOvertime'], $_REQUEST['bDriving'], $_REQUEST['iClientgroup']);
			if ($iResult <= 0) {
				$bError = true;
			}
		}
		if (!$bError) {
			header('Location: index.php?sView='.$_REQUEST['sView']);
			exit();
		}
		break;
	case 'time-sendtime':
		TimeHandler::sendTime($_SESSION['rid'], $_REQUEST['iTime']);
		break;
	case 'time-sendmonth':
		TimeHandler::sendMonth($_SESSION['rid'], $_REQUEST['iYear'], $_REQUEST['iMonth']);
		break;
}
?>
